package com.xd.clap.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xd.clap.entity.Classify;
import com.xd.clap.entity.response.Result;
import com.xd.clap.mapper.ClassifyMapper;
import com.xd.clap.services.ClassifyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Resource
    private ClassifyMapper cm;

    @Override
    public Result getClassifyList(){
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        List<Classify> classifyList = cm.selectList(queryWrapper);

        return Result.success(classifyList,"加载成功");
    }

    @Override
    public Result updateClassify(Classify classify) {
        if (classify.getId() == null){
            int insert = cm.insert(classify);
            if (insert < 1) {
                return Result.fail(0, "填加失败");
            }
            return Result.success(insert, "添加成功！");
        }else{
            int update = cm.updateById(classify);
            if (update < 1) {
                return Result.fail(0, "更新失败");
            }
            return Result.success(update, "更新成功！");
        }

    }

    @Override
    public Result deleteClassify(int id) {
        int delete = cm.deleteById(id);
        if (delete < 1) {
            return Result.fail(0, "删除失败");
        }
        return Result.success(delete, "删除成功！");
    }

}
